import { FairyGUI, UnityEngine } from "csharp";

/**
 * 对象管理
 * @author clong 2020.11.22
 */
export default class ObjectUtils{

    public static showObject( data:Object , parentKey:string = "" , showValue:boolean = true ):void{

        if(!data) return;
        let key;
        for( key in data ){
            // if( typeof data[key] === "object" ){
            //     ObjectUtils.showObject( data[key] , key );
            // }else{
                if( parentKey ){
                    console.log( "[ObjectUtils.showObject]----->parentKey: "+parentKey+" key:" + key + ( showValue ? " value: " +  data[key] : "" ) );
                }else{
                    console.log( "[ObjectUtils.showObject]----->key:" + key + ( showValue ? " value: " +  data[key] : "" ) );
                }                
            // }            
        }
    }

    public static getObjectKeys( o:Object | Laya.WeakObject ):number{

		let count:number = 0;
		if( o instanceof Laya.WeakObject ){
			o = o["_obj"];
		}
		for( let key in o ){
			count++;
		}
		return count;
	}
    
    /**
     * 当前点是否在元件范围内
     * @param target 
     * @param gp    全局坐标，默认为鼠标当前坐标
     */
    public static globalIsIn(target: FairyGUI.GObject, gp: Laya.Point|Laya.Vector2 = null): boolean {

        if (gp == null) {
            gp = new Laya.Point(Laya.stage.mouseX, Laya.stage.mouseY);
        }
        if( target == null ){
            return false;
        }
        let targetGP:UnityEngine.Vector2 = target.LocalToGlobal(new UnityEngine.Vector2(0,0));//当前坐标的全局坐标
        if (target.pivotAsAnchor) {
            let hw: number = target.width * 0.5;
            let hh: number = target.height * 0.5;
            if (gp.x >= targetGP.x - hw && gp.x <= (targetGP.x + hw) && gp.y >= targetGP.y - hh && gp.y <= (targetGP.y + hh)) {
                return true;
            }
        } else {
            if (gp.x >= targetGP.x && gp.x <= (targetGP.x + target.width) && gp.y >= targetGP.y && gp.y <= (targetGP.y + target.height)) {
                return true;
            }
        }
        return false;
	}

    /**
	 * 获取对象类名
	 * @param target 
	 */
	public static getClassName( target:any ):string{

		if( target && target.constructor.name === "Function" ){
			if( target.name ){
				return target.name;
			}else{
				console.error( "ObjectUtils.getClassName 错误!!!" );
			}
		}		
		return target && target.constructor.name;
    }
    
    /**
     * 获取对应父容器的类对象
     * @param cls 
     * @param self 
     */
    public static getParentOf( cls:any , self:FairyGUI.GObject|Laya.Node = null ):any{

        let parent = self ? self.parent : null;
        let p:any = null;
        if( parent != null ){
            if( parent instanceof cls ){
                return parent;
            }else{
                return this.getParentOf( cls , parent );
            }            
        }
        return null;
	}

	/**
     * 获取对象是否有对应名字的父容器
     * @param cls 
     * @param self 
	 * @param isMatchAll 是否全词匹配
     */
    public static getParentByName( self:FairyGUI.GObject|Laya.Node , name:string|Array<string> = "Dibiao" , isMatchAll:boolean = false ):any{

        let parent = self ? self.parent : null;
        let p:any = null;
        if( parent != null ){
			if( name instanceof Array ){
				if( name.indexOf(parent.name) != -1 ){
					return parent;
				}else if( !isMatchAll ){
					for( let i=0;i<name.length;i++ ){
						if( parent.name.indexOf(name[i]) != -1 ){
							return parent;
						}
					}
				}
				return this.getParentByName( parent , name );
			}else if( isMatchAll && parent.name == name ){
				return parent;
			}
            else if( !isMatchAll && parent.name.indexOf(name) != -1 ){
                return parent;
            }else{
                return this.getParentByName( parent , name );
            }            
        }
        return null;
	}

	/**
     * 元件是否有空的父容器，由此判断该元件是否在舞台
     * @param obj
     */
    public static getObjNullParent( obj:FairyGUI.GObject ):boolean{

        if( obj ){			
			if( obj.parent == null ){
				return true;
			}else{
				if( obj instanceof FairyGUI.GRoot || obj.parent instanceof FairyGUI.GRoot ){
					return false;
				}
				return ObjectUtils.getObjNullParent( obj.parent );
			}
		}
        return true;
	}
}